home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / reve / trans / trans.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  9.0 KB  |  322 lines

  1.  
  2. /*  @(#)trans.c 1.3 91/03/20
  3.  *
  4.  *  Copyright (C) 1991 - Valerie Haecky.
  5.  *  All rights reserved.
  6.  * 
  7.  *  Permission is granted to copy this source, for redistribution 
  8.  *  in source form only, provided the news headers in "substantially 
  9.  *  unaltered format" are retained, the introductory messages are not 
  10.  *  removed, and no monies are exchanged. 
  11.  * 
  12.  *  Permission is also granted to copy this source, without the 
  13.  *  news headers, for the purposes of making an executable copy by 
  14.  *  means of compilation, provided that such copy will not be used 
  15.  *  for the purposes of competition in any othello tournaments, without 
  16.  *  prior permission from the authors. 
  17.  * 
  18.  *  No responsibility is taken for any errors on inaccuracies inherent 
  19.  *  either to the comments or the code of this program, but if reported 
  20.  *  (see README file), then an attempt will be made to fix them. 
  21.  */ 
  22.  
  23. #include <stdio.h>
  24. #include <sys/param.h>
  25. #include <ctype.h>
  26.  
  27. #define FCLOSE  (void) fclose
  28. #define FPRINTF (void) fprintf
  29. #define PRINTF  (void) printf
  30. #define PUTC    (void) putc
  31. #define SPRINTF (void) sprintf
  32.  
  33. #define TRUE  1    
  34. #define FALSE 0             
  35.  
  36. #define BLACK 0
  37. #define WHITE 1
  38. #define EMPTY ""
  39.  
  40. extern void exit() ;
  41.  
  42. #ifndef  PROLOGUE       /* Trans PostScript prologue file location. */
  43. #define  PROLOGUE "/usr/local/lib"
  44. #endif /*PROLOGUE*/
  45.  
  46. #ifndef  MAXPATHLEN
  47. #define  MAXPATHLEN   1024
  48. #endif /*MAXPATHLEN*/
  49.  
  50. FILE * gamefp;        /* File containing ASCII transcript */
  51. FILE * trfp;        /* File for PostScript transcript */
  52. FILE * psfp;        /* Template PostScript File */
  53.  
  54. int infile = FALSE;
  55. int outfile = FALSE;
  56.  
  57. char * blackPlayer = EMPTY;
  58. char * whitePlayer = EMPTY;
  59. char * result = EMPTY;
  60. char * date = EMPTY;
  61. char * place = EMPTY;
  62. char * comment = EMPTY;
  63. char * gamename = "reve.game";
  64. char * transname = "reve.trans.ps";
  65. char * scale = "1";
  66. char * ptime = EMPTY;
  67.  
  68.  
  69. /* Copy the template file into the transcript file */
  70. int
  71. initTranscript()
  72. {
  73.    char c, tpsname[MAXPATHLEN];
  74.    if (outfile)
  75.    {
  76.       if ((trfp = fopen(transname, "w")) == NULL)
  77.       {
  78.      FPRINTF(stderr,"\n\tERROR:Destination file %s can't be opened\n", transname);
  79.      return(-1);
  80.       }
  81.    } else
  82.    {
  83.       trfp = stdout;
  84.    }
  85.  
  86.    /* put the first things into the transcript file */
  87.    PUTC('%', trfp);
  88.    FPRINTF(trfp, "! \n\n");
  89.  
  90.    /* put the definitions into the transcript file */
  91.    FPRINTF(trfp, "/Scale {%s %s} def \n", scale, scale);
  92.    FPRINTF(trfp, "/BlackPlayer (%s) def \n", blackPlayer);
  93.    FPRINTF(trfp, "/WhitePlayer (%s) def \n", whitePlayer);
  94.    FPRINTF(trfp, "/Score (%s) def \n", result);
  95.    FPRINTF(trfp, "/Date (%s) def \n", date);
  96.    FPRINTF(trfp, "/Place (%s) def \n", place);
  97.    FPRINTF(trfp, "/Time (%s) def \n", ptime);
  98.    FPRINTF(trfp, "/Comments (%s) def \n\n", comment);
  99.  
  100.    /* copy the template into the transcript file */
  101.    PUTC('%',trfp);
  102.    FPRINTF(trfp, " --------- Start imported trans.ps --------\n\n");
  103.    SPRINTF(tpsname, "%s/trans.ps", PROLOGUE);
  104.    if ((psfp = fopen(tpsname, "r")) == NULL)
  105.    {
  106.       PRINTF ("\n\tERROR: File trans.ps not found.\n");
  107.       return(-1);
  108.    }
  109.    while ((c = getc(psfp)) != EOF) PUTC(c,trfp); /* copy the template */
  110.    FCLOSE(psfp);
  111.    PUTC('%',trfp);
  112.    FPRINTF(trfp, " --------- End imported trans.ps --------\n\n");
  113.    return(0);
  114. }
  115.  
  116. void
  117. drawStone(move, square, color)
  118.   int move, square, color;
  119. {
  120.   FPRINTF(trfp, "  %d %d %d drawStone \n", move, square, color);
  121. }
  122.  
  123.  
  124. void
  125. endTranscript()
  126. {
  127.    /* write final grestore and showpage into transcript */ 
  128.    FPRINTF(trfp, "grestore \n\n");
  129.    FPRINTF(trfp, "showpage \n\n");
  130.    FCLOSE(trfp);
  131. }
  132.  
  133.  
  134. /* This function interprets the gamefile. It is heavily dependend on
  135.  * the current format of the gamefile. If that format changes, this
  136.  * function needs to be rewritten (nothing else);
  137.  */
  138. void drawMoves()
  139. {
  140.     char   c;            /* Last character read from gamefp */
  141.     int    move;        /* Number of current move */
  142.     int    color;        /* Color of current move */
  143.     int    square;        /* numerical value of square of move */
  144.  
  145.     
  146.     while ((c = getc(gamefp)) != EOF) 
  147.         {
  148.         if (!isdigit(c))
  149.             {
  150.             while (c != '\n') c = getc(gamefp);
  151.             }
  152.         else
  153.             {
  154.             /* First we get the move number */
  155.             /* First character is always a number */
  156.             move = c - '0';
  157.  
  158.             /* Second character is a number or a coma. */
  159.             if ((c = getc(gamefp)) != ',')
  160.                 {
  161.                 move = 10*move +  c - '0';
  162.                 c = getc(gamefp); /* get the coma */
  163.                 }
  164.  
  165.             /* next we have spaces */
  166.             while ((c = getc(gamefp)) == ' ');
  167.   
  168.             /* now we get a '-' or a '<' */
  169.             if (c  == '-')
  170.                 {
  171.                 color = WHITE;
  172.                 while ((c = getc(gamefp)) != '<');
  173.                 }
  174.             else
  175.                 color = BLACK;
  176.   
  177.             /* next we read alpha-dash-number for square */
  178.             /* convert the alpha to a number and ignore the dash */
  179.             c = getc(gamefp);
  180.             if (isupper(c))
  181.                 square = c - 'A' + 1; 
  182.             else 
  183.                 square = c - 'a' + 1;
  184.             c = getc(gamefp);
  185.             c = getc(gamefp); 
  186.             square = 10*square + c - '0';
  187.   
  188.             /* then read to EOL */
  189.             while ((c = getc(gamefp)) != ']');
  190.             c = getc(gamefp); 
  191.   
  192.             /* and draw the move */
  193.             drawStone(move, square, color);
  194.             }/*end else*/
  195.         }/*end while*/
  196. }
  197.  
  198.  
  199. main(argc,argv)
  200.     int        argc;
  201.     char    **argv;
  202. {
  203.    /* interpret the command line arguments */
  204.  
  205.   if (argc != 1)
  206.     {
  207.     argv++;  /* point to the first argument */
  208.     while (*argv != NULL)
  209.       {  
  210.       if (strcmp (*argv, "-b") == 0)
  211.           {
  212.           argv++;
  213.           blackPlayer = *argv;
  214. /*          FPRINTF(stderr,"Black: %s \n", blackPlayer); */
  215.           }
  216.       else if (strcmp (*argv, "-w") == 0)
  217.           {
  218.           argv++;
  219.           whitePlayer = *argv;
  220.          FPRINTF(stderr,"White: %s \n", whitePlayer);
  221.           }
  222.       else if (strcmp (*argv, "-r") == 0)
  223.           {
  224.           argv++;
  225.           result = *argv;
  226.          FPRINTF(stderr,"Result: %s \n", result);
  227.           }
  228.       else if (strcmp (*argv, "-d") == 0)
  229.           {
  230.           argv++;
  231.           date = *argv;
  232.          FPRINTF(stderr,"Date: %s \n", date);
  233.           }
  234.       else if (strcmp (*argv, "-c") == 0)
  235.           {
  236.           argv++;
  237.           comment = *argv;
  238.          FPRINTF(stderr,"Comment: %s \n", comment);
  239.           }
  240.       else if (strcmp (*argv, "-s") == 0)
  241.           {
  242.           argv++;
  243.           scale = *argv;
  244.          FPRINTF(stderr,"Scale Factor: %s \n", scale);
  245.           }
  246.       else if (strcmp (*argv, "-g") == 0)
  247.           {
  248.           argv++; infile = TRUE ;
  249.           gamename = *argv;
  250.          FPRINTF(stderr,"Game File: %s \n", gamename);
  251.           }
  252.       else if (strcmp (*argv, "-l") == 0)
  253.           {
  254.           argv++;
  255.           ptime = *argv;
  256.          FPRINTF(stderr,"Time Limit: %s \n", ptime);
  257.           }
  258.       else if (strcmp (*argv, "-p") == 0)
  259.           {
  260.           argv++;
  261.           place = *argv;
  262.          FPRINTF(stderr,"Place: %s \n", place);
  263.           }
  264.       else if (strcmp (*argv, "-t") == 0)
  265.           {
  266.           argv++; outfile = TRUE ;
  267.           transname = *argv;
  268.          FPRINTF(stderr,"Transcript File: %s \n", transname);
  269.           }
  270.       else if (strcmp (*argv, "-h") == 0)
  271.           {
  272.          FPRINTF(stderr,"\n\tUseage: trans [options|-h] \n\n");
  273.          FPRINTF(stderr,"\tOptions:\n\n");
  274.          FPRINTF(stderr,"\t  -b string   black player \n");
  275.          FPRINTF(stderr,"\t  -w string   white player \n");
  276.          FPRINTF(stderr,"\t  -r string   result/score \n");
  277.          FPRINTF(stderr,"\t  -d string   date \n");
  278.          FPRINTF(stderr,"\t  -c string   comment \n");
  279.          FPRINTF(stderr,"\t  -g string   game file path \n");
  280.          FPRINTF(stderr,"\t  -t string   transcript file path \n");
  281.          FPRINTF(stderr,"\t  -s string   scaling factor for transcript \n");
  282.          FPRINTF(stderr,"\t  -p string   place or occasion \n");
  283.          FPRINTF(stderr,"\t  -l string   time limit \n");
  284.          FPRINTF(stderr,"\t  -h          display this help mesg \n\n");
  285.          FPRINTF(stderr," To print your transcript, send the transcript \n");
  286.          FPRINTF(stderr," file to any PostScript printer. \n\n");
  287.          FPRINTF(stderr," For more help, read the README file. \n\n");
  288.           return(3);
  289.           }
  290.       else
  291.           {
  292.          FPRINTF(stderr,"ERROR: Invalid argument list.\n");
  293.          FPRINTF(stderr,"       Type 'trans -h' for help \n\n");
  294.           return(4);
  295.           }
  296.       argv++;
  297.       }/*endwhile*/
  298.     }/*endif*/
  299.  
  300.     /* open the appropriate files for reading and writing */
  301.     if (infile)
  302.     {
  303.     
  304.     if ((gamefp = fopen(gamename, "r")) == NULL) 
  305.         {
  306.        FPRINTF(stderr,"\n\tERROR: Source file %s cannot be opened\n", gamename);
  307.         return(5);
  308.         }
  309.     } else
  310.     {
  311.     gamefp = stdin ;
  312.     }
  313.  
  314.     if (initTranscript() != 0) return(-1);
  315.     drawMoves();
  316.     endTranscript();
  317.  
  318.     FCLOSE(gamefp);
  319.     exit(0);
  320. /*NOTREACHED*/
  321. }
  322.